package service;

import java.util.List;

import object.Tour;

public class TourService 
{
	public void print(List<Tour> tours)
	{
		for (Tour tour : tours) 
		{
			System.out.println(tour.toString());
		}
	}
	
	public void addTour(List<Tour> tours, String surname)
	{
		Tour tour = new Tour();
		ConsoleService ser = new ConsoleService();
		
		tour.setType(ser.getType());
		
		if (surname.isEmpty())
			tour.setClient(ser.getSurnameWithExit());
		else
			tour.setClient(surname);
			
		tour.setHotel(ser.getHotel());
		tour.setDate(ser.getDateAsString());
		tour.setId(tours.size() + 1);
		
		tours.add(tour);
	}
	
	public void removeTour(List<Tour> tours)
	{
		ConsoleService ser = new ConsoleService();
		
		if (tours.isEmpty())
		{
			System.out.println("There are no data to remove!");
			ser.delay();
			return;
		}
		
		int indexToRemove = ser.getIndexToRemove();
		
		if (indexToRemove > 0 && indexToRemove <= (tours.size() + 1))
		{
			indexToRemove--;
			
			tours.remove(indexToRemove);
			
			updateTours(tours);
		}
		else
		{
			System.out.println("Tour not found!");
		}
	}
	
	public boolean findBySurname(List<Tour> tours, String surname)
	{
		for (Tour tour : tours) 
		{
			if (tour.getClient().compareToIgnoreCase(surname) == 0)
				return true;
		}
		
		return false;
	}
	
	private void updateTours(List<Tour> tours)
	{
		for (int i = 0; i < tours.size(); i++) 
		{
			tours.get(i).setId(i + 1);
		}
	}
}
